package com.vivz.baby.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import com.vivz.baby.enumeration.LoginType;

@Entity
@Table(name = "USER")
public class User extends AbstractEntity {
	
	private static final long serialVersionUID = 1L;
	
	@Column(name = "EMAIL")
	private String email;
	
	@Column(name = "CREDENTIALS")
	private String credentials;
	
	@Column(name = "LOGIN_TYPE_ID")
	private Long loginTypeId;
	
	// @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
	// private final List<Log> logs = new ArrayList<Log>();
	
	public String getEmail() {
		return email;
	}
	
	public void setEmail(final String email) {
		this.email = email;
	}
	
	public String getCredentials() {
		return credentials;
	}
	
	public void setCredentials(final String credentials) {
		this.credentials = credentials;
	}
	
	public LoginType getLoginType() {
		return LoginType.getInstance(loginTypeId.intValue());
	}
	
	public void setLoginType(final LoginType loginType) {
		loginTypeId = Long.valueOf(loginType.getId());
	}
	
	// public List<Log> getLogs() {
	// return logs;
	// }
	//
	// public void setLogs(final List<Log> logs) {
	// this.logs.clear();
	// if (CollectionUtils.isNotEmpty(logs)) {
	// this.logs.addAll(logs);
	// }
	// }
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder(59, 7) //
				.append(getId()) //
				.append(email) //
				.append(loginTypeId) //
				.hashCode();
	}
	
	@Override
	public boolean equals(final Object o) {
		
		if (o instanceof User) {
			final User that = (User) o;
			return new EqualsBuilder() //
					.append(getId(), that.getId()) //
					.append(email, that.email) //
					.append(loginTypeId, that.loginTypeId) //
					.isEquals();
		}
		
		return false;
	}
	
	@Override
	public String toString() {
		return new ToStringBuilder(this) //
				.append(getId()) //
				.append(email) //
				.append(getLoginType()) //
				.toString();
	}
	
}
